﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TB_WS.WEB
{
    public partial class Auth_RT : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            string nick = "";
            Label1.Text = "当前链接："+Request.Url.ToString();
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string CanShu_Str = Request.Url.ToString().Split('?')[1].ToString();
            string[] CanShu = CanShu_Str.Split('&');
            for (int i = 0; i < CanShu.Length; i++)
            {
                if (CanShu[i].Split('=')[0].ToString() == "nick")
                {
                    nick = CanShu[i].Split('=')[1].ToString();
                }
                else
                {
                    dic.Add(CanShu[i].Split('=')[0].ToString(), CanShu[i].Split('=')[1].ToString());
                }
            }
            int Nick_Count = Class.QianNiu.get_QN_Nick_Count(nick);
            if(Nick_Count==0)
            {
                //还没有这个ID入驻
                Response.Redirect("https://oauth.taobao.com/authorize?client_id=23595706&response_type=code&redirect_uri=http://lstwechat.59igou.com/web/authorize.aspx&state=1212&view=web");
            }
            else
            {
                //已经存在了 检查SessionKey
                Int64 Old_Session_EX_Time = Convert.ToInt64(Class.QianNiu.Get_QN_Info(nick).Tables[0].Rows[0][3].ToString());
                if(Old_Session_EX_Time> LeShiTao_Library.Tools.Data_Handle.GET_UNIX_TIMESTAMP())
                {
                    //还在有效期内
                    Response.Redirect("http://lstwechat.59igou.com/web/MainForm.aspx?Nick="+nick);
                }
                else
                {
                    //session过期
                    Response.Redirect("https://oauth.taobao.com/authorize?client_id=23595706&response_type=code&redirect_uri=http://lstwechat.59igou.com/web/authorize.aspx&state=1212&view=web");
                }
            }
        }
        public static string FormatBizQueryParaMap(Dictionary<string, string> paraMap, bool urlencode)
        {
            string buff = "";
            try
            {
                var result = from pair in paraMap orderby pair.Key select pair;

                foreach (KeyValuePair<string, string> pair in result)
                {
                    if (pair.Key != "")
                    {
                        string key = pair.Key;
                        string val = pair.Value;
                        if (urlencode)
                        {
                            val = System.Web.HttpUtility.UrlEncode(val);
                        }
                        buff += key.ToLower()+val;

                    }
                }

                if (buff.Length == 0 == false)
                {
                    buff = buff.Substring(0, (buff.Length - 1) - (0));
                }
            }
            catch (Exception e)
            {
                //throw new SDKRuntimeException(e.Message);
            }
            return buff;
        }
    }
}